/*
 * Copyright (c) 2017. 杭州端点网络科技有限公司.  All rights reserved.
 */

package com.jiantong.user.impl.manager;

import com.jiantong.user.impl.dao.BreedFactoryDao;
import com.jiantong.user.impl.dao.ButcherFactoryDao;
import com.jiantong.user.impl.dao.GovernmentDao;
import com.jiantong.user.impl.dao.OrganizationDao;
import com.jiantong.user.impl.dao.OrganizationUserDao;
import com.jiantong.user.impl.dao.UserDao;
import com.jiantong.user.model.BreedFactory;
import com.jiantong.user.model.ButcherFactory;
import com.jiantong.user.model.Government;
import com.jiantong.user.model.Organization;
import com.jiantong.user.model.OrganizationUser;
import com.jiantong.user.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author : panxin
 */
@Component
public class OrganizationManager {

    private final OrganizationDao organizationDao;
    private final ButcherFactoryDao butcherFactoryDao;
    private final BreedFactoryDao breedFactoryDao;
    private final GovernmentDao governmentDao;
    private final UserDao userDao;
    private final OrganizationUserDao organizationUserDao;

    @Autowired
    public OrganizationManager(OrganizationDao organizationDao, ButcherFactoryDao butcherFactoryDao, BreedFactoryDao breedFactoryDao, GovernmentDao governmentDao, UserDao userDao, OrganizationUserDao organizationUserDao) {
        this.organizationDao = organizationDao;
        this.butcherFactoryDao = butcherFactoryDao;
        this.breedFactoryDao = breedFactoryDao;
        this.governmentDao = governmentDao;
        this.userDao = userDao;
        this.organizationUserDao = organizationUserDao;
    }

    @Transactional
    public <T extends Organization> Long createGovernment(Organization organization, T organizationInstance, User user, OrganizationUser organizationUser) {
        // 用户基本信息
        userDao.create(user);
        Long userId = user.getId();
        String userName = user.getName() + userId;

        // 修改用户名
        User toUpdateUser = new User();
        toUpdateUser.setId(userId);
        toUpdateUser.setName(userName);
        userDao.update(toUpdateUser);

        // 机构主信息
        organization.setUserId(userId);
        organizationDao.create(organization);
        Long id = organization.getId();

        // 详细信息
        organizationInstance.setId(id);
        organizationInstance.setUserId(userId);
        if (organizationInstance instanceof Government) {
            governmentDao.create((Government) organizationInstance);
        }
        if (organizationInstance instanceof ButcherFactory) {
            butcherFactoryDao.create((ButcherFactory) organizationInstance);
        }
        if (organizationInstance instanceof BreedFactory) {
            breedFactoryDao.create((BreedFactory) organizationInstance);
        }

        // 关联信息
        organizationUser.setUserId(userId);
        organizationUser.setUserName(userName);
        organizationUser.setOrganizationId(id);
        organizationUserDao.create(organizationUser);

        return id;
    }

}
